package com.ftl.framework;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

public class BitmapImg {

	private Bitmap bitmap;
	
	
	public Bitmap getBitmap()
	{
		return this.bitmap;
	}
	
	public void setBitmap(Bitmap pBitmap)
	{
		this.bitmap = pBitmap;
		
	}
	
	public BitmapImg ()
	{
		this.bitmap = null;
	}
	public BitmapImg (Bitmap pBitmap)
	{
		this.bitmap = pBitmap;
	}
	public BitmapImg (Resources res,int i)
	{
		this.bitmap = BitmapFactory.decodeResource(res,i);
	}
	public BitmapImg (String pathName)
	{
		try{
		this.bitmap = BitmapFactory.decodeFile(pathName);
		}catch(Exception e)
		{
			Log.d("Error en BitmapImg (String pathName)",e.getMessage());
		}
	}
	
	
	public boolean mergeBitmap(BitmapImg s)
	{
		return this.mergeBitmap(s.getBitmap());
	}
    public boolean mergeBitmap(Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
        Bitmap cs = null; 
     
        int width, height = 0; 
        
        width = ((this.bitmap.getWidth() > s.getWidth()) ? this.bitmap.getWidth() : s.getWidth());
        height = ((this.bitmap.getHeight() > s.getHeight()) ? this.bitmap.getHeight() : s.getHeight());
        
        float x = ((this.bitmap.getWidth() > s.getWidth()) ? (width / 2) - (s.getWidth()/2) : (width / 2) - (this.bitmap.getWidth()/2));
        float y = ((this.bitmap.getHeight() > s.getHeight()) ? (height / 2) - (s.getHeight()/2) : (height / 2) - (this.bitmap.getHeight()/2));
        
        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     
        Canvas comboImage = new Canvas(cs); 
     
        comboImage.drawBitmap(this.bitmap, 0f, 0f, null); 
        comboImage.drawBitmap(s, x, y, null); 
     
        this.bitmap = cs;
        
        return true;
        
      }	
    

    
    public boolean save(String pathName)
    {
    	OutputStream os = null; 
        try { 
          os = new FileOutputStream(pathName); 
          this.bitmap.compress(CompressFormat.PNG, 100, os); 
        } catch(IOException e) { 
          Log.e("combineImages", "problem combining images", e); 
          return false;
        }    	
    	
    	
    	return true;
    }
	
}
